﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Javavaj.iHunter.Core
{
    internal class PluginWrapper : IPlugin
    {
        IPlugin plugin;

        public IPlugin Plugin
        {
            get { return plugin; }
        }

        public PluginWrapper(IPlugin plugin)
        {
            try
            {
                this.plugin = plugin;
            }
            catch (Exception e)
            {
                OnPluginError(e);
            }
            this.plugin.SettingChanged += new PluginSettingChangedEventHandler(plugin_SettingChange);
        }

        void plugin_SettingChange(object sender, PluginSettingChangedEventArgs e)
        {
            if (this.SettingChanged != null)
                this.SettingChanged.Invoke(sender, e);
        }

        #region IPlugin 成员

        public void InitSettings(System.Xml.XmlDocument settings)
        {
            try
            {
                this.plugin.InitSettings(settings);
            }
            catch (Exception e)
            {
                OnPluginError(e);
            }
        }

        public Type GetType()
        {
            return plugin.GetType();
        }
        public void Config()
        {
            try
            {
                this.plugin.Config();
            }
            catch (Exception e)
            {
                OnPluginError(e);
            }
        }

        public event PluginSettingChangedEventHandler SettingChanged;

        #endregion

        protected void OnPluginError(Exception e)
        {
            if (PluginError != null)
                PluginError.Invoke(this,new PluginErrorEventArgs(new PluginInformation(),e));
        }

        internal event PluginErrorEventHandler PluginError;
    }
}
